<template>
<!--  修改个人消息-->
<div id="cp-div" >
      <div class="cp-box">
        <div style="padding-top: 15px;">
          <h1 >个人中心</h1>
        </div>
        <div style="display: flex;margin-bottom: 20px">
          <h3>用户名:&nbsp;&nbsp;</h3>
          <div style="width:350px">
            <el-input   :maxlength="8" v-model="userName" placeholder="用户名长度不超过8位" ></el-input>
          </div>
        </div>
        <div style="display: flex;margin-bottom: 20px">
          <h3 style="margin-right: 2px">&nbsp;&nbsp;&nbsp;姓名:&nbsp;&nbsp;</h3>
          <div style="width:350px">
            <el-input  :minlength='2' :maxlength="8" v-model="realName" placeholder="姓名长度不超过8位" ></el-input>
          </div>
        </div>
        <div style="display: flex;margin-bottom: 20px">
          <h3 style="margin-right: 2px">&nbsp;&nbsp;&nbsp;性别:&nbsp;&nbsp;</h3>
          <div style="width:350px;margin-left: 10px;padding-top: 10px">
            <el-radio v-model="sex" label="男">男</el-radio>
            <el-radio v-model="sex" label="女">女</el-radio>
          </div>
        </div>
          <div style="display: flex;margin-bottom: 20px">
          <h3 style="margin-right: 2px">&nbsp;&nbsp;&nbsp;邮箱:&nbsp;&nbsp;</h3>
          <div style="width:350px">
            <el-input :maxlength="25" v-model="email" ></el-input>
          </div>
        </div>
        <div style="display: flex;margin-bottom: 20px">
          <h3 style="margin-right: 2px">&nbsp;&nbsp;&nbsp;电话:&nbsp;&nbsp;</h3>
          <div style="width:350px">
            <el-input  :minlength='11' :maxlength="11" v-model="phone"  oninput = "value=value.replace(/[^\d]/g,'')" ></el-input>
          </div>

        </div>


        <div style="padding-top: 20px;margin-left: 72px">
          <el-button @click="changeMsg2" :loading="changeMsg" type="primary" style="width: 350px">确认修改</el-button>
        </div>
      </div>
</div>

</template>

<script>
import {get,post} from "../../utils/request";

export default {
  name: "ChangePassword",
  data(){
    return{
      sex:null,
      userName:null,
      realName:null,
      email:null,
      phone:null,
      changeMsg:false
    }
  },
  created() {
    get("/api/v1/token/test2").then(resp=>{
    })
    get("/api/v1/user/change/msg/detail").then(resp=>{
      this.userName=resp.data[0].user_name
      this.realName=resp.data[0].real_name
      this.email=resp.data[0].email
      this.phone=resp.data[0].phone
      this.sex=resp.data[0].sex
    })

  },
  methods:{
    changeMsg2(){
      if(this.userName===""||this.userName===null){
        this.$message.error("用户名不能为空")
        return
      }
      if (this.realName===""||this.realName===null){
        this.$message.error("姓名不能为空")
        return;
      }
      if(this.email===""||this.email===null){
        this.$message.error("邮箱不能为空")
        return;
      }
      if (this.phone===""||this.phone===null){
        this.$message.error("电话不能为空")
        return;
      }

      this.checkThisName()

    },
    checkThisName(){
      this.changeMsg=true
      post("/api/v1/user/check/name",this.userName).then(resp=>{
        if (resp.data){
          this.$message.error("用户名已经重复了，请重新填写用户名")
          this.changeMsg=false
        }else {
          this.updateUserMsg()
        }

      })
    },
    updateUserMsg(){

      var user={
        userName:this.userName,
        realName:this.realName,
        sex:this.sex,
        email:this.email,
        phone:this.phone
      }
      post('/api/v1/update/user/msg',user).then(resp=>{
        if (resp.data.code===1){
          this.$message({
            message: resp.data.msg,
            type: 'success'
          });
          this.changeMsg=false
          this.$emit("updateTheTitleUserName")
          this.$router.push({name:"MainMsg"})
        }else {
          this.changeMsg=false
          this.$message.error(resp.data.msg)
        }
      }).catch(resp=>{
        this.changeMsg=false
        this.$message.error("系统出错了，请重试")
      })
    }

  }
}
</script>

<style scoped>
h3{
  margin: 6px 0 0;
}
#cp-div{
  background-color: #f2f4fa;
  width: 100%;
  height: 692px;

}
.cp-box{
  margin-top: 40px;
  margin-left: 60px;
  width: 91%;
  height: 600px;
  padding-left: 17px;
  background-color: white;
  border-radius: 30px;
  box-shadow:  20px 20px 60px #bebebe,
  -20px -20px 60px #ffffff;
}
</style>
